<?php



declare (strict_types=1);



namespace app\beautiful\controller\cashier;

use app\BaseController;

use app\beautiful\model\CouponModel;
use app\beautiful\model\GiftbagModel;
use app\beautiful\model\LevelModel;

use app\beautiful\model\mall\GoodsCategoryModel;

use app\beautiful\model\mall\GoodsModel;

use app\beautiful\model\member\MemberBalanceModel;

use app\beautiful\model\member\MemberCouponModel;

use app\beautiful\model\member\MemberIntegralModel;

use app\beautiful\model\member\MemberModel;

use app\common\logic\DataLogic;
use think\facade\Db;


class Cash extends BaseController{

    public function getGoodsList()

    {



        $name=$this->request->param('name');

        $category_id=$this->request->param('search_category_id/d',0);

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['status','=',1],

            ['quota','>',0]

        ];

        if(!empty($name)) $where[]=['name','like',"%{$name}%"];

        if(!empty($category_id)) {

            $categoryIds = GoodsCategoryModel::where('pid',$category_id)->whereOr('category_id',$category_id)->column('category_id');
            $where[]=['category_id','in',$categoryIds];
        }



        $goods_model= new GoodsModel();

        $goods_model=$goods_model->with(['sku'=>function($query){

            $query->where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0],

                ['quota','>',0],

                ['is_delete','=',0]

            ]);

        }]);

        $order=['sort'=>'desc','update_time'=>'desc','goods_id'=>'desc'];

        $field="*";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($goods_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);



        if($this->request->page==1){

            $category= GoodsCategoryModel::where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0],

            ])->order(['sort'=>'desc','add_time'=>'desc'])

                ->field('category_id,name,pid')

                ->select()->toArray();

            $return['type_list']=$category;

        }

        return $this->success($return);

    }



    public function getChooseMemberList()

    {

        $keyword=$this->request->param('keyword');

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

        ];



        if(!empty($keyword)){

            if(false === strpos($keyword, '_')){

                $where[]=['mobile','=',$keyword];

            }else{

                $keyword_arr=explode("_", $keyword);

                $member_id=$keyword_arr[0];

                $where[]=['member_id','=',$member_id];

            }

        }





        $memberModel=new MemberModel();

        $memberModel=$memberModel->with(['level'=>function($query){

            $query->field('level_id,name,discount');

        }]);

        $order=['add_time'=>'desc'];

        $append=['add_time_format'];

        $field='member_id,nick_name,face,mobile,level_id,add_time';

        $data=DataLogic::getDataList($memberModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);



        //处理数据

        $lists=$data['list'];

        if(empty(!$lists)){

            $memberIds = $balances = $integrals = [];

            foreach($lists as $val){

                $memberIds[$val['member_id']] = $val['member_id'];

            }

            if(!empty($memberIds)){

                $MemberBalanceModel = new MemberBalanceModel();

                $balances =  $MemberBalanceModel->getBalanceSumByMemberId($memberIds);

                $MemberIntegralModel = new MemberIntegralModel();

                $integrals = $MemberIntegralModel->getIntegralSumByMemberId($memberIds);

            }

            foreach ($lists as $k => $list){

                $lists[$k]['balance']=$balances[$list['member_id']] ?? 0;

                $lists[$k]['integral']=$integrals[$list['member_id']] ?? 0;

                if(empty($list['level'])){
                    $level['discount']=10;
                    $level['level_id']=0;
                    $level['name']='普通会员';
                    $lists[$k]['level']=$level;
                }

            }

        }

        $data['list']=$lists;

        return $this->success($data);

    }



    public function getChooseCouponList()

    {

        $member_id = $this->request->param('member_id/d',0);

        $total_price=$this->request->param('total_price/f',0);

        $today = date('Y-m-d', $this->request->time());

        $where = [

            ['mc.shop_id','=',$this->request->shopId],

            ['mc.member_id','=', $member_id],

            ['mc.status','=',0],

            ['mc.valid_end_time','>=',$today],

            ['mc.money_limit','<=',$total_price]

        ];

        $field="mc.member_coupon_id,mc.coupon_id,mc.member_id,mc.name,mc.valid_bg_time,mc.valid_end_time,mc.coupon_money,
        mc.money_limit,mc.status,FROM_UNIXTIME(mc.add_time) as add_time_format ";

        $order=['mc.member_coupon_id' => 'desc'];

        $return = [];

        $return['total'] = Db::name('beautiful_member_coupon')->alias('mc')
            ->join('wyu_beautiful_coupon c','mc.coupon_id=c.coupon_id and c.type_id !=5')
            ->field("c.type_id as coupon_type_id,$field")
            ->where($where)->count();

        $return['list'] = Db::name('beautiful_member_coupon')->alias('mc')
            ->join('wyu_beautiful_coupon c','mc.coupon_id=c.coupon_id and c.type_id !=5')
             ->field("c.type_id as coupon_type_id,$field")
            ->where($where)
            ->order($order)
            ->limit($this->request->limit)->page($this->request->page)
            ->select()
            ->toArray();


        return $this->success($return);

    }

}

